//
//  LoginViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/3/19.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "LoginViewController.h"
#import "PhoneOrPwdView.h"
#import "MessageCodeViewController.h"
#import "RegisterViewController.h"
#import "ForgetPwdViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>

/** 号码 */
@property (nonatomic,strong) PhoneOrPwdView *phoneView;
/** 密码 */
@property (nonatomic,strong) PhoneOrPwdView *pwdView;

@end

@implementation LoginViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"登录";
    self.wb_leftButton.hidden = YES;
    
    [self setUpUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:WB_RGBColor(0, 0, 0),NSForegroundColorAttributeName,[UIFont fontWithName:@"HiraginoSans-W3" size:18],NSFontAttributeName, nil]];
    //设置导航栏背景图片为一个空的image，这样就透明了
    [self.navigationController.navigationBar setBackgroundImage:[Factory createImageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    [WB_SharedApplication setStatusBarStyle:UIStatusBarStyleDefault];
    
}

- (void)setUpUI
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight + 1);
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
    [scrollView addGestureRecognizer:singleTap];
    [self.view addSubview:scrollView];
    
    //logo图片
    UIImageView *logoIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    logoIV.center = CGPointMake(kScreenWidth / 2, 90);
    logoIV.image = WB_ImageName(@"logo");
    [scrollView addSubview:logoIV];
    
    //手机号码
    PhoneOrPwdView *phoneView = [[NSBundle mainBundle] loadNibNamed:@"PhoneOrPwdView" owner:self options:nil].firstObject;
    phoneView.imageIV.image = WB_ImageName(@"用户名-grey");
    phoneView.inputTF.keyboardType = UIKeyboardTypeNumberPad;
    phoneView.frame = CGRectMake(0, logoIV.mm_bottom + 50, kScreenWidth, 55);
    phoneView.inputTF.placeholder = @"请输入手机号码";
    phoneView.inputTF.delegate = self;
    _phoneView = phoneView;
    [scrollView addSubview:phoneView];
    
    //密码
    PhoneOrPwdView *passwordView = [[NSBundle mainBundle] loadNibNamed:@"PhoneOrPwdView" owner:self options:nil].firstObject;
    passwordView.imageIV.image = WB_ImageName(@"密码-grey");
    passwordView.inputTF.secureTextEntry = YES;
    passwordView.frame = CGRectMake(0, phoneView.mm_bottom, kScreenWidth, 55);
    passwordView.inputTF.placeholder = @"请输入6～16为密码(包含字母和数字)";
    passwordView.inputTF.delegate = self;
    _pwdView = passwordView;
    [scrollView addSubview:passwordView];
    
    //登录
    UIButton *loginBtn = [Factory customButtonWithTitle:@"登录"];
    loginBtn.tag = 1000;
    loginBtn.frame = CGRectMake(35, passwordView.mm_bottom + 50, kScreenWidth - 70, 60);
    [loginBtn addTarget:self action:@selector(clickMethod:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:loginBtn];
    
    //立即注册
    UIButton *registBtn = [UIButton ba_buttonWithFrame:CGRectMake(loginBtn.mm_left + 10, loginBtn.mm_bottom, 100, 30) title:@"立即注册" titleColor:WB_RGBColor(150, 150, 150) titleFont:[UIFont systemFontOfSize:14]];
    registBtn.tag = 1001;
    registBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [registBtn addTarget:self action:@selector(clickMethod:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:registBtn];
    
    //忘记密码
    UIButton *forgetPwdBtn = [UIButton ba_buttonWithFrame:CGRectMake(loginBtn.mm_right - 100 - 10, loginBtn.mm_bottom, 100, 30) title:@"忘记密码?" titleColor:WB_RGBColor(150, 150, 150) titleFont:[UIFont systemFontOfSize:14]];
    forgetPwdBtn.tag = 1002;
    forgetPwdBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [forgetPwdBtn addTarget:self action:@selector(clickMethod:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:forgetPwdBtn];
    
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"叉"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  style:UIBarButtonItemStylePlain target:self action:@selector(clickMethod:)];

    leftBarItem.tag = 1003;
    self.navigationItem.leftBarButtonItem = leftBarItem;
}

#pragma mark - textField Delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (_phoneView.inputTF == textField) {
        _phoneView.imageIV.image = WB_ImageName(@"用户名_blue");
        _phoneView.lineView.backgroundColor = WB_RGBColor(79, 155, 250);
    }else if (_pwdView.inputTF == textField){
        _pwdView.imageIV.image = WB_ImageName(@"密码_blue");
        _pwdView.lineView.backgroundColor = WB_RGBColor(79, 155, 250);
    }
    NSLog(@"获得焦点");
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (_phoneView.inputTF == textField) {
        _phoneView.imageIV.image = WB_ImageName(@"用户名-grey");
        _phoneView.lineView.backgroundColor = WB_RGBColor(221, 221, 221);
    }else if (_pwdView.inputTF == textField){
        _pwdView.imageIV.image = WB_ImageName(@"密码-grey");
        _pwdView.lineView.backgroundColor = WB_RGBColor(221, 221, 221);
    }
    NSLog(@"失去焦点");
}

- (void)clickMethod:(UIButton *)sender
{
//            MessageCodeViewController *vc = [[MessageCodeViewController alloc] init];
//            [self.navigationController pushViewController:vc animated:YES];
//    return;
    if (sender.tag == 1000) {
        if (WB_IsNilString(_phoneView.inputTF.text)) {
            [NSObject wb_showAlertWithTitle:@"账号不能为空"];
            return;
        }else if (![Factory isPhoneNumber:_phoneView.inputTF.text]){
            [NSObject wb_showAlertWithTitle:@"请输入正确格式的手机号"];
            return;
        }else if (WB_IsNilString(_pwdView.inputTF.text)){
            [NSObject wb_showAlertWithTitle:@"密码不能为空"];
            return;
        }else if (_pwdView.inputTF.text.length<6){
            [NSObject wb_showAlertWithTitle:@"密码必须大于或等于6位字符"];
            return;
        }
        WB_WeakSelf(self)
        [XYHttpRequest dataRequestLoginWithPhone:_phoneView.inputTF.text withPassword:_pwdView.inputTF.text success:^(id responseObj) {
            if (WB_IsEquallString(responseObj[@"responseCode"], @"1")) {
                [NSObject wb_showAlertWithTitle:@"登录成功!" duration:DelayedTime];
                [[NSUserDefaults standardUserDefaults] setObject:responseObj[@"token"] forKey:Token];
                [[NSUserDefaults standardUserDefaults] setObject:weakself.phoneView.inputTF.text forKey:AppUserPhone];
                
                [[NSUserDefaults standardUserDefaults] setObject:responseObj[@"data"][@"user"][@"id"] forKey:CustId];
                [[NSUserDefaults standardUserDefaults] synchronize];
                WB_DISPATCH_MAIN_Delay_THREAD(1.5, ^{
                    [self dismissViewControllerAnimated:YES completion:nil];
                })
            }else{
                [NSObject wb_showAlertWithTitle:responseObj[@"info"]];
            }
        } failure:^(NSError *error) {
        }];

    }else if (sender.tag == 1001) {
        RegisterViewController *vc = [[RegisterViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (sender.tag == 1002) {
        ForgetPwdViewController *vc = [[ForgetPwdViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (sender.tag == 1003) {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer
{
    UIScrollView *scrollView = (UIScrollView *)gestureRecognizer.view;
    [scrollView endEditing:YES];
}

@end
